#!/usr/bin/perl
use strict;
package modes;
use modules::encode;
sub new {
    my $add; my $remove; my $count; my $tmp; my @modes;
#$input[4] is blank if usermode.
    my $dbh = shift;
    my $sth;
   ##########
   #POINTERS#
   ##########
    my $cbans = shift;
    my $input = shift;
   ##########
    if ($$input[4] eq "") {
	if ($$input[3] =~ /\+o/) {
	    $sth = $dbh->prepare("UPDATE ninfo SET oper = '1' WHERE num = '$$input[0]';");
	    $sth->execute;
	}
	if ($$input[3] =~ /\-o/) {
	    $sth = $dbh->prepare("UPDATE ninfo SET oper = '0' WHERE num = '$$input[0]';");
	    $sth->execute;
	}
    }



    else {
	
#Az M #maxismedia +o ABAAA
#input 4 - 9
	my $count = 0;
	my @nickstock;
	$nickstock[0] = $$input[4];
	$nickstock[1] = $$input[5];
	$nickstock[2] = $$input[6];
	$nickstock[3] = $$input[7];    
	$nickstock[4] = $$input[8];
	$nickstock[5] = $$input[9];


	undef $tmp;
	@modes = split(//,$$input[3]);
	foreach my $i (@modes) {
	    if ($i eq '+') {
		$add=1;
		$remove=0;
	    }
	    elsif ($i eq '-') {
		$add = 0;
		$remove = 1;
	    }
	    else {
		if ($add == 1 && $remove == 0) {
		    print "Going into addmode - $i\n";
		    if ($i eq 'o') {
			my $encode = encode::encode($nickstock[$count]);
			print "update onchan set isop = '1' where num = '$nickstock[$count]' AND channel = '$$input[2]' AND code = '$encode';\n";

			$sth = $dbh->prepare("update onchan set isop = '1' where num = '$nickstock[$count]' AND channel = '$$input[2]' AND code = '$encode';");
			undef $encode;
			$sth->execute;
			$count++;
		    }
		    elsif ($i eq 'v') {

			$sth = $dbh->prepare("UPDATE onchan SET isvoice = '1' WHERE num = '$nickstock[$count]' AND channel = '$$input[2]' AND code = '".encode::encode($nickstock[$count])."';");
			$sth->execute; 
			$count++;
		    }
		    elsif ($i eq 'b') {
			#isban $nickstock[$count]
			$$cbans{$$input[2]}->{lc($nickstock[$count])} = 1;
			$count++;
		    }
		    elsif ($i eq 'k') {
			$sth=$dbh->prepare("INSERT INTO ckey VALUES ('$$input[2]','$nickstock[$count]');");
			$sth->execute;
			$count++;
		    }
		    elsif ($i eq 'l') {
			$count++;
		    }
		    else {
			$count++;
		    }

		}
	    
		if ($remove == 1 && $add == 0) {
		    if ($i eq 'o') {
		  

			$sth = $dbh->prepare("UPDATE onchan set isop = '0' WHERE num = '$nickstock[$count]' AND channel = '$$input[2]' AND code = '".encode::encode($nickstock[$count])."';");
			$sth->execute;
			$count++;
		    }
		    elsif ($i eq 'v') {
			$sth = $dbh->prepare("UPDATE onchan SET isvoice = '0' WHERE num = '$nickstock[$count]' AND channel = '$$input[2]' AND code = '".encode::encode($nickstock[$count])."';");
			$sth->execute;
			$count++;
		    }
		    elsif ($i eq 'b') {
			delete($$cbans{$$input[2]}->{lc($nickstock[$count])});
			$count++;
		    }
		    elsif ($i eq 'k') {
			$sth=$dbh->prepare("DELETE FROM ckey WHERE channel = '$$input[2]';");
			$sth->execute;
			$count++;
		    }
		    elsif ($i eq 'l') {
     
		    }
		    else {
			$count++;
		    }
		}
	    }
	}
    }
}
				
				
1;
    
